function getFilterData() {
  var instance = le_1.l;
  instance.i.Ka = gameVersion;
  const data = instance.W.nc(instance.i);
  var result = {
    rules: data.rules,
    gameVersion: data.gameVersion,
    gameVersionBase: data.gameVersionBase,
  };

  return result;
}

function setFilterData(data) {
  userLootFilters = [{
    "id": "aBZjJDB4",
    "title": data.title || 'new filter',
    "description": "",
    "icon": "0,0",
    "rules": data.rules || [],
    "version": 1,
    "createdDate": "2024-03-15 18:48:42",
    "updatedDate": "2024-03-15 18:48:42",
    "gameVersion": data.gameVersion || window.gameVersion,
    "authorId": 31636,
    "classes": [],
    "skills": [],
    "tags": [],
    "privacy": 0,
    "source": 0,
    "originalId": "",
    "gameVersionBase": data.gameVersionBase || window.gameVersion,
  }];
  userLootFilterTemplates = [];
  le_kg.init();
  le_6j = 0;
  le_8j();
}

function innerSetFilter(result) {
  var instance = le_1.l;
  var data = instance.W.nc(result);
  setFilterData(data);
  return data;
}

function changeTitle(data) {
  userLootFilters[0].title = data.title;
  setFilterData(userLootFilters);
}

function setTitle(data) {
  window.parent.postMessage({
    source: 'd2core',
    action: 'changeTitle',
    payload: {
      title: data.title,
    },
  }, '*');
}

function importFilterData(fileContent) {
  try {
    var result = le_7e(null, fileContent);
    if (result === null) {
      throw new Error('文件解析失败');
    }
    var data = innerSetFilter(result);
    setTitle(data);
  } catch (error) {
    alert(error.message);
    console.error(error);
  }
}

function showExport() {
  $('.export-filter-button').click();
}

function showGuide(callback) {
  le_1.j.I.show();
  le_1.j.I.callbacks.qd = function(result) {
    var data = innerSetFilter(result);
    setTitle(data);
  };
}

window.addEventListener('message', function (e) {
  if (!e.data) return;
  var source = e.data.source;
  var action = e.data.action;
  var payload = e.data.payload;
  if (source !== 'd2core' || (e.origin.indexOf('localhost') === -1 && e.origin.indexOf('d2core.com') === -1)) return;
  if (action === 'getFilterData') {
    var data = getFilterData();
    e.source.postMessage({
      source: source,
      action: action,
      payload: {
        data: data,
        callback: payload.callback,
      },
    }, '*');
  } else if (action === 'importFilterData') {
    importFilterData(payload);
  } else if (action === 'setFilterData') {
    setFilterData(payload);
  } else if (action === 'showExport') {
    showExport(payload);
  } else if (action === 'changeTitle') {
    changeTitle(payload);
  } else if (action === 'showGuide') {
    showGuide(payload);
  }
});